package z_oldnofunc;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.StringReader;
import java.net.URL;
import java.net.URLConnection;
import org.jdom2.Content;
import org.jdom2.JDOMException;
import org.jdom2.input.SAXBuilder;
import org.jdom2.util.IteratorIterable;

public class ParserPruebaDBLP_XmlToBIb_Version1 {

	public static void main(String[] args) {
		try {
			
			String finalHTML = "";
			finalHTML = cargarURL("http://dblp.org/search/api/?q=software*%20agile*%20test*&h=1000&c=4&f=0&format=xml");
			String xml = finalHTML;
			SAXBuilder saxBuilder = new SAXBuilder();
			try {
				org.jdom2.Document doc = saxBuilder.build(new StringReader(xml
						.trim()));
				System.out.println("URL!");
				System.out.println(doc.getProperty("url"));
				IteratorIterable<Content> it = doc.getDescendants();
				String bib = "";

				String tipo = "";
				String autor = "";
				String titulo = "";
				String lugar = "";
				String url = "";
				String ano = "";

				while (it.hasNext()) {
					Content content = (Content) it.next();
					if (content.toString().contains("<type/>")) {
						tipo = content.getValue();						
					}
					if (content.toString().contains("<authors/>")) {
						autor = content.getValue();
					}
					if (content.toString().contains("<title/>")) {
						titulo = content.getValue();
					}
					if (content.toString().contains("<venue/>")) {
							lugar = content.getValue();						
					}
					if (content.toString().contains("<url/>")) {
						url = content.getValue();
					}
					if (content.toString().contains("<year/>")) {
						ano = content.getValue();
					}

					if (!tipo.equals("") && !autor.equals("")
							&& !titulo.equals("") && !lugar.equals("")
							&& !url.equals("") && !ano.equals("")) {
						
						String tag = url.substring(url.lastIndexOf('/')+1, url.length()-1);
						
						bib += "\n@" + tipo + "{" + tag +",\n";
						bib += "author = {" + autor + "},\n";
						bib += "title = {{" + titulo + "}},\n";
						bib += "venue = {" + lugar + "},\n";
						bib += "url = {" + url + "},\n";
						bib += "year = {" + ano + "}\n}";
						tipo = "";
						autor = "";
						titulo = "";
						lugar = "";
						url = "";
						ano = "";
					}
				}
				System.out.println(bib);				
				System.out.println("Fin procesamiento");
				//FileWriter salida = new FileWriter(new File("C:/Users/Usuario/Desktop/salidaLibreriaDBLP.bib"));
				File f = new File("salidaLibreriaDBLP.bib");				
				FileWriter salida = new FileWriter(f);
				salida.write(bib);
				salida.flush();
				salida.close();
				System.out.println("Archivo Guardado en : " + f.getAbsolutePath());

			} catch (JDOMException e) {
				// handle JDOMException
				System.out.println(e);
			} catch (IOException e) {
				// handle IOException
				System.out.println(e);
			}

		} catch (IOException e2) {
			System.out.println(e2);
			e2.printStackTrace();
		}
	}

	public static String cargarURL(String dir) throws IOException {
		URL url = new URL(dir);
		URLConnection spoof;
		spoof = url.openConnection();
		// Spoof the connection so we look like a web browser
		spoof.setRequestProperty("User-Agent",
				"Mozilla/4.0 (compatible; MSIE 5.5; Windows NT 5.0;    H010818)");
		BufferedReader in;
		in = new BufferedReader(new InputStreamReader(spoof.getInputStream()));
		String strLine = "";
		String finalHTML = "";
		while ((strLine = in.readLine()) != null) {
			finalHTML += strLine;
		}
		in.close();
		return finalHTML;
	}

}
